<? if (!defined("BASEPATH")) exit("No direct script access allowed");

//Common::pr($getInstitute);

// Country
if(isset($getCityInstitute) and count($getCityInstitute)) {

	//Common::pr($getCityInstitute);
?>
	<script type="text/javascript">

		$(window).load(function() {

			<? /* var mapcenter = new google.maps.LatLng('<?=$getCountry[0]["couLatitude"]?>','<?=$getCountry[0]["couLongitude"]?>'); */ ?>
			var mapcenter = new google.maps.LatLng('<?=$getCityInstitute[key($getCityInstitute)]["cityLatitude"]?>','<?=$getCityInstitute[key($getCityInstitute)]["cityLongitude"]?>');
			var bounds = new google.maps.LatLngBounds();
			var mapOptions = {
				<? $cnt = count($getCityInstitute); ?>
				<?=(($cnt == 1)?("zoom: 6,"):(""))?>
				center: mapcenter,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				language: "en"
			};

			var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
			var infowindow = new google.maps.InfoWindow();

			<? foreach($getCityInstitute as $cityId => $cityData) { ?>
			var point = new google.maps.LatLng('<?=$cityData["cityLatitude"]?>', '<?=$cityData["cityLongitude"]?>');
			var marker<?=$cityId?> = new google.maps.Marker({
					position: point,
					map: map,
					title:'<?=$this->db->escape_str($cityData["cityName"])?>'
			});
			<?
				$content = "<div>".anchor($cityData["conUrlName"]."/".$cityData["couUrlName"]."/".$cityData["cityUrlName"], "<strong>".$cityData["cityName"]."</strong>", "", $forced = TRUE)."</div>";
				if(isset($cityData["ins"])) {
					$content .= "<ul>";
					foreach($cityData["ins"] as $insId => $insData) {
						$content .=		"<li>".anchor("institutes/".$insData["insUrlName"], $insData["insName"])."</li>";
					}
					$content .= "</ul>";
				}
			?>
			google.maps.event.addListener(marker<?=$cityId?>, 'click', function() {
				infowindow.setContent('<div><?=$this->db->escape_str($content)?></div>');
				infowindow.open(map, marker<?=$cityId?>);  
			});
			bounds.extend(point);
			<? } ?>

			<? if($cnt > 1) { ?>
			map.fitBounds(bounds);
			<? } ?>

		})
	</script>
<?

}


// City
//Common::pr($getInstitute);exit;
if(isset($getCity) and $getCity[0]["cityLatitude"] != "" and isset($getInstitute)) { ?>

	<script type="text/javascript">

		$(window).load(function() {

			var mapcenter = new google.maps.LatLng('<?=$getCity[0]["cityLatitude"]?>','<?=$getCity[0]["cityLongitude"]?>');
			var bounds = new google.maps.LatLngBounds();
			var mapOptions = {
				<?=((!count($getInstitute))?("zoom: 6,"):(""))?>
				center: mapcenter,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				language: "en"
			};
			bounds.extend(mapcenter);

			var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
			var infowindow = new google.maps.InfoWindow();

			<? if(!count($getInstitute)) { ?>
				var point = mapcenter;
				var marker = new google.maps.Marker({
						position: point,
						map: map,
						title:'<?=$this->db->escape_str($getCity[0]["cityName"])?>'
				});
			<? } ?>

			<? 
			$voltIns = FALSE;
			foreach($getInstitute as $ii => $insData) { 
				if(!$insData["insLatitude"]) {
					continue;
				} else {
					$voltIns = TRUE;
				}
			?>
			var point = new google.maps.LatLng('<?=$insData["insLatitude"]?>', '<?=$insData["insLongitude"]?>');
			var marker<?=$ii?> = new google.maps.Marker({
					position: point,
					map: map,
					title:'<?=$this->db->escape_str($insData["insName"])?>'
			});
			<?
				$content = "<div>".anchor("institutes/".$insData["insUrlName"], $insData["insName"])."</div>".(($insData["insAddress"])?("<div>".$insData["insAddress"].", ".$getCity[0]["cityName"].", ".$getCity[0]["couName"]."</div>"):(""));
			?>
			google.maps.event.addListener(marker<?=$ii?>, 'click', function() {
				infowindow.setContent('<div><?=$this->db->escape_str($content)?></div>');
				infowindow.open(map, marker<?=$ii?>);  
			});
			bounds.extend(point);
			<? } ?>

			<? if($voltIns) { ?>
			map.fitBounds(bounds);
			<? } ?>

		})
	</script>

<? }

// Institute
elseif(isset($getInstitute) and isset($getInstitute[0]) and $getInstitute[0]["insLatitude"] != "") { ?>

	<script type="text/javascript">

		$(window).load(function() {

			var mapcenter = new google.maps.LatLng('<?=$getInstitute[0]["insLatitude"]?>','<?=$getInstitute[0]["insLongitude"]?>');
			var bounds = new google.maps.LatLngBounds();
			var mapOptions = {
				<? $cnt = count($getInstitute); ?>
				center: mapcenter,
				<?=(($cnt == 1)?("zoom: 15,"):(""))?>
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				language: "en"
			};
			bounds.extend(mapcenter);

			var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
			var infowindow = new google.maps.InfoWindow();

			<? foreach($getInstitute as $ii => $insData) { ?>
				var point = new google.maps.LatLng('<?=$insData["insLatitude"]?>', '<?=$insData["insLongitude"]?>');
				var marker<?=$ii?> = new google.maps.Marker({
						position: point,
						map: map,
						title:'<?=$this->db->escape_str($insData["insName"])?>'
				});
				<?
					$content = "<div>".anchor("institutes/".$insData["insUrlName"], $insData["insName"])."</div>";
				?>
				google.maps.event.addListener(marker<?=$ii?>, 'click', function() {
					infowindow.setContent('<div><?=$this->db->escape_str($content)?></div>');
					infowindow.open(map, marker<?=$ii?>);  
				});
				bounds.extend(point);
			<? } ?>

			<?
			if(isset($getInstituteAddresses) and $getInstituteAddresses) {
				foreach($getInstituteAddresses as $ii => $address) {
			?>
					var point = new google.maps.LatLng('<?=$address->add_latitude?>', '<?=$address->add_longitude?>');
					var marker<?=($ii+1)?> = new google.maps.Marker({
						position: point,
						map: map,
						title:'<?=$this->db->escape_str($address->add_name)?>'
					});
					<?
						$content = "<div>".$address->add_name.": ".trim($address->city->city_name." ".$address->add_postal_code.", ".$address->add_address)."</div>";
					?>
					google.maps.event.addListener(marker<?=($ii+1)?>, 'click', function() {
						infowindow.setContent('<div><?=$this->db->escape_str($content)?></div>');
						infowindow.open(map, marker<?=($ii+1)?>);
					});
					bounds.extend(point);
			<?
				}
			}
			?>

			<? if($cnt > 1) { ?>
			map.fitBounds(bounds);
			<? } ?>

		})
	</script>

<? }

// Accomodation
elseif(isset($getAccomodation) and isset($getAccomodation[0]) and $getAccomodation[0]["accLatitude"] != "") { ?>

	<script type="text/javascript">

		$(window).load(function() {

			var mapcenter = new google.maps.LatLng('<?=$getAccomodation[0]["accLatitude"]?>','<?=$getAccomodation[0]["accLongitude"]?>');
			var bounds = new google.maps.LatLngBounds();
			var mapOptions = {
				center: mapcenter,
				zoom: 15,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				language: "en"
			};
			bounds.extend(mapcenter);

			var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
			var infowindow = new google.maps.InfoWindow();

			var point = new google.maps.LatLng('<?=$getAccomodation[0]["accLatitude"]?>', '<?=$getAccomodation[0]["accLongitude"]?>');
			var marker = new google.maps.Marker({
					position: point,
					map: map,
					title:'<?=$this->db->escape_str($getAccomodation[0]["accName"])?>'
			});
			bounds.extend(point);

		})
	</script>

<? }